package de.bht.mi.kriz.multitouch.components;

/**
 * based on http://www.anddev.org/basic_drag_and_drop-t3095.html
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;

public class ColorBall {
 private Bitmap img; // the image of the ball
 private float coordX = 0.0f; // the x coordinate at the canvas
 private float coordY = 0.0f; // the y coordinate at the canvas
 private int id; // gives every ball his own id, for now not necessary
 private static int count = 1;
 private boolean goRight = true;
 private boolean goDown = true;
 private boolean goLeft = true;
 private boolean goUp = true;

 
 
 /* 
  * Animation control members
  */
 
 private View callingView;
 private boolean moving = false;
 private Interpolator animateInterpolator;  
 private long startTime;  
 private long endTime;  
 private float totalAnimDx;  
 private float totalAnimDy;
 
	public String toString() {
		return this.getX()+" "+this.getY();
	}
	
	public ColorBall(View view, Context context, int drawable, Point point) {

		callingView = view;

		BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        img = BitmapFactory.decodeResource(context.getResources(), drawable); 
        id=count;
		count++;
		coordX= point.x;
		coordY = point.y;

	}
	
	public static int getCount() {
		return count;
	}
	
	public void setX(float f) {
        coordX = f;
    }
	
	public float getX() {
		return coordX;
	}
	
	public float getDrawX() {
		return coordX - this.getBitmap().getWidth() / 2;
	}	

	public void setY(float newValue) {
        coordY = newValue;
   }
	
	public float getY() {
		return coordY;
	}

	public float getDrawY() {
		return coordY - this.getBitmap().getHeight() / 2;
	}		
	
	public int getID() {
		return id;
	}
	
	public Bitmap getBitmap() {
		return img;
	}
	
	public void moveBall(float dx, float dy, boolean bounce) {
		// check the borders, and set the direction if a border has reached
		
		Log.d("func_moveBall", "x:"+coordX+" y:"+coordY+" dx:"+dx+" dy:"+dy);
		
		if(bounce)
		{
			if (coordX+dx > callingView.getWidth() - getBitmap().getWidth()/2){
				goLeft = true;
				goRight = false;
			}
			if (coordX+dx < 0 + getBitmap().getWidth()/2){
				goLeft = false;
				goRight = true;
			}
			if (coordY+dy > callingView.getHeight() - getBitmap().getHeight()/2){
				goUp = true;
				goDown = false;
			}
			if (coordY+dy < 0 + getBitmap().getHeight()/2){
				goUp = false;
				goDown = true;
			}
			// move the x and y 
			if (goRight && dx > 0 || goLeft && dx < 0){
				coordX += dx;
			} else {
				coordX -= dx;
			}
			
			if (goDown && dy > 0 || goUp && dy < 0){
				coordY += dy;
			} else {
				coordY -= dy;
			}
		}
		else
		{
			if(coordX+dx > callingView.getWidth() - getBitmap().getWidth()/2)
			{
				coordX = callingView.getWidth() - getBitmap().getWidth()/2;
			} else if(coordX+dx < 0 + getBitmap().getWidth()/2) {
				coordX = 0;
			} else {
				coordX += dx;
			}
			
			if(coordY+dy > callingView.getHeight() - getBitmap().getHeight()/2)
			{
				coordY = callingView.getHeight() - getBitmap().getHeight()/2;
			} else if(coordY+dy < 0 + getBitmap().getHeight()/2) {
				coordY = 0;
			} else {
				coordY += dy;
			}
		}
		
	}
	
	public void onAnimateMove(float dx, float dy, long duration, final boolean bounce) {  
	    if(!moving) 
	    {
	    	goRight = goDown = goLeft = goUp = true;
	    	moving = true;
	    	
	    	animateInterpolator = new AccelerateDecelerateInterpolator();  
		    startTime = System.currentTimeMillis();  
		    endTime = startTime + duration;  
		    totalAnimDx = dx;  
		    totalAnimDy = dy;  
		    callingView.post(new Runnable() {  
		        @Override  
		        public void run() {  
		            onAnimateStep(bounce);  
		        }  
		    });  
	    }
	    else
	    {
		    endTime =  System.currentTimeMillis() + duration;  
		    totalAnimDx = dx;  
		    totalAnimDy = dy;  
	    }
	}  
	
	private void onAnimateStep(final boolean bounce) {  
	    long curTime = System.currentTimeMillis();  
	    float percentTime = (float) (curTime - startTime)  / (float) (endTime - startTime);  
	    float percentDistance = animateInterpolator.getInterpolation(percentTime);  
	    float curDx = percentDistance * totalAnimDx;  
	    float curDy = percentDistance * totalAnimDy;  
	    onMove(curDx, curDy, bounce);  
	  
	    Log.v("func_onAnimateStep", "We're " + percentDistance + " of the way there!");  
	    if (percentTime < 1.0f) {  
	    	callingView.post(new Runnable() {  
	            @Override  
	            public void run() {  
	                onAnimateStep(bounce);  
	            }  
	        });  
	    } else {
	    	moving = false;
	    }
	    
	} 
	
    public void onMove(float dx, float dy, boolean bounce) {  
        moveBall(dx, dy, bounce);  
        callingView.invalidate();  
    }  
	
}
